package b1.players
{
	import b1.conifg.*;
	import b1.net.*;
	
	public class AnimalInfo
	{
		//唯一号
		public var id:int;
		//动物的种类索引
		public var animalIndex:int;
		//动物名字
		public var animalName:String;
		//所有者名字
		public var ownerName:String;
		//经验值		
		public var experience:int;
		//等级
		public var level:int;
		//最大生命值
		private var _healthPointMax:int;
		//当前生命值
		public var healthPoint:int;
		//最大体力值
		private var _vitalityPointMax:int;
		//当前体力值
		public var vitalityPoint:int;
		//智力值
		private var _intelligence:int;
		//速度 
		private var _speed:Number;
		//未分配的技能点
		public var skillPoint:int;
		//所有者的用户Id（原籍）
		public var ownerId:int;
		//目前所住的位置（住址）
		public var liveId:int;
		//出狱的时间（秒）
		public var timeToGetFree:int;
		//上次同步保存生命和体力的时间
		public var hpSaveTime:int;
		//动物的所有线索（按线索类型clueIndex作为索引）
		public var animalClues:Object;
		//被通缉的骨头
		public var wantedBones:int;
		//被通缉的截止时间
		public var timeToClose:int;
		//动物的所有装备（按部位position作为索引）
		public var equips:Object;
		//魔法冷却回合
		public var cooldown:Object;
		//自定义参数
		public var userData1:int;
		
		public function AnimalInfo(animalXML:XML = null)
		{
			if (animalXML != null)
			{
				this.id = parseInt(animalXML.animalId);
				this.animalIndex = parseInt(animalXML.animalIndex);
				this.animalName = animalXML.animalName;
				var levelExp:Array = getLevelByExp(parseInt(animalXML.experience));
				this.level = levelExp[0];
				this.experience = levelExp[1];
				this.ownerName = animalXML.ownerName;
				this._healthPointMax = parseInt(animalXML.healthPointMax);
				this.healthPoint = parseInt(animalXML.healthPoint);
				this._vitalityPointMax = parseInt(animalXML.vitalityPointMax);
				this.vitalityPoint = parseInt(animalXML.vitalityPoint);
				this._intelligence = parseInt(animalXML.intelligence);
				this._speed = parseFloat(animalXML.speed);
				this.skillPoint = parseInt(animalXML.skillPoint);
				this.ownerId = parseInt(animalXML.ownerId);
				this.liveId = parseInt(animalXML.liveId);
				this.timeToGetFree = parseInt(animalXML.timeToGetFree);
				this.hpSaveTime = parseInt(animalXML.hpSaveTime);
				this.animalClues = new Object();
				
				for (var i:int = 0; i < animalXML.clue.length(); i++)
				{
					var clueIndex:int = parseInt(animalXML.clue[i].clueIndex);
					var clueValue:int = parseInt(animalXML.clue[i].clueValue);
					this.animalClues[clueIndex] = clueValue;
				}
				this.equips = new Object();
				for (i = 0; i < animalXML.equip.length(); i++)
				{
					var equip:EquipInfo = new EquipInfo(animalXML.equip[i]);
					this.equips[equip.position] = equip;
				}
				this.cooldown = new Object();
			}
		}
		
		public function resetParams(healthPointMax:int, vitalityPointMax:int, speed:Number, intelligence:int):void
		{
			this._healthPointMax = healthPointMax;
			this._vitalityPointMax = vitalityPointMax;
			this._speed = speed;
			this._intelligence = intelligence;
		}
		
		public static function getLevelByAddExp(oldLevel:int, oldExp:int, addExp:int):Array
		{
			var level:int = oldLevel;
			var experience:int = oldExp + addExp;
			while (experience >= (Config.levelConfigs[level - 1] as LevelConfig).experience)
			{
				experience -= (Config.levelConfigs[level - 1] as LevelConfig).experience; 
				level++;
			}
			return [level, experience];
		}
		
		public static function getLevelByExp(experience:int):Array
		{
			return getLevelByAddExp(1, 0, experience);
		}
		
		//得到套装的整体配置，返回null，如果没有收集齐套装
		public function getWholeEquipConfig():EquipConfig
		{
			var equipCount:int = 0;
			var lastEquipConfig:EquipConfig = null;
			var equipConfig:EquipConfig;
			for (var position:String in equips)
			{
				var equip:EquipInfo = equips[position];
				equipConfig = Config.objectConfigs[equip.objectIndex];
				if (lastEquipConfig == null || equipConfig.equipType == lastEquipConfig.equipType)
				{
					equipCount++;
					lastEquipConfig = equipConfig;
				}
			}
			//套装整体作用
			if (equipConfig != null && equipCount >= equipConfig.equipNum)
			{
				return equipConfig;
			}
			else
			{
				return null;
			}
		}
		
		public function get currentHealthPoint():int
		{
			var animalConfig:AnimalConfig = Config.animalConfigs[animalIndex];
			var currentHp:int = healthPoint + animalConfig.regainRate * (ServerHelper.getInstance().now() - hpSaveTime);
			return Math.min(currentHp, healthPointMax);
		}
		
		public function get currentVitalityPoint():int
		{
			var animalConfig:AnimalConfig = Config.animalConfigs[animalIndex];
			var currentVp:int = vitalityPoint + animalConfig.regainRate * (ServerHelper.getInstance().now() - hpSaveTime);
			return Math.min(currentVp, vitalityPointMax);
		}
		
		public function get healthPointByEquip():int
		{
			var hpByEquip:int = 0;
			for (var position:String in equips)
			{
				var equip:EquipInfo = equips[position];
				var equipConfig:EquipConfig = Config.objectConfigs[equip.objectIndex];
				hpByEquip += equipConfig.addHealthPoint + equipConfig.colorAddHealthPoint;
			}
			return hpByEquip;
		}
		
		public function get healthPointByEquipWhole():int
		{
			var wholeEquipConfig:EquipConfig = getWholeEquipConfig();
			if (wholeEquipConfig != null)
			{
				return wholeEquipConfig.addHealthPointWhole;
			}
			else
			{
				return 0;
			}
		}
		
		public function get healthPointMax():int
		{
			return _healthPointMax + healthPointByEquip + healthPointByEquipWhole;
		}
		
		public function get vitalityPointByEquip():int
		{
			var vpByEquip:int = 0;
			for (var position:String in equips)
			{
				var equip:EquipInfo = equips[position];
				var equipConfig:EquipConfig = Config.objectConfigs[equip.objectIndex];
				vpByEquip += equipConfig.addVitalityPoint + equipConfig.colorAddVitalityPoint;
			}
			return vpByEquip;
		}
		
		public function get vitalityPointByEquipWhole():int
		{
			var wholeEquipConfig:EquipConfig = getWholeEquipConfig();
			if (wholeEquipConfig != null)
			{
				return wholeEquipConfig.addVitalityPointWhole;
			}
			else
			{
				return 0;
			}
		}
		
		public function get vitalityPointMax():int
		{
			return _vitalityPointMax + vitalityPointByEquip + vitalityPointByEquipWhole;
		}
		
		public function get intelligenceByEquip():int
		{
			var itByEquip:int = 0;
			for (var position:String in equips)
			{
				var equip:EquipInfo = equips[position];
				var equipConfig:EquipConfig = Config.objectConfigs[equip.objectIndex];
				itByEquip += equipConfig.addIntelligence + equipConfig.colorAddIntelligence;
			}
			return itByEquip;
		}
		
		public function get intelligenceByEquipWhole():int
		{
			var wholeEquipConfig:EquipConfig = getWholeEquipConfig();
			if (wholeEquipConfig != null)
			{
				return wholeEquipConfig.addIntelligenceWhole;
			}
			else
			{
				return 0;
			}
		}
		
		public function get intelligence():int
		{
			return this._intelligence + intelligenceByEquip + intelligenceByEquipWhole;
		}
		
		public function get speedByEquip():int
		{
			var spByEquip:int = 0;
			for (var position:String in equips)
			{
				var equip:EquipInfo = equips[position];
				var equipConfig:EquipConfig = Config.objectConfigs[equip.objectIndex];
				spByEquip += equipConfig.addSpeed + equipConfig.colorAddSpeed;
			}
			return spByEquip;
		}
		
		public function get speedByEquipWhole():int
		{
			var wholeEquipConfig:EquipConfig = getWholeEquipConfig();
			if (wholeEquipConfig != null)
			{
				return wholeEquipConfig.addSpeedWhole;
			}
			else
			{
				return 0;
			}
		}
		
		public function get speed():int
		{
			return this._speed + speedByEquip + speedByEquipWhole;
		}


	}
}